package com.shyroke.daydayzhuanweb.controller;


import com.shyroke.daydayzhuanapi.dto.ProjectDto;
import com.shyroke.daydayzhuanapi.entity.ProjectEntity;
import com.shyroke.daydayzhuanapi.entity.VersionEntity;
import com.shyroke.daydayzhuandao.respository.VersionRepository;
import com.shyroke.daydayzhuansvc.manager.ProjectManager;
import com.shyroke.daydayzhuansvc.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用于安卓请求controller
 */
@RestController
@RequestMapping(value = "api")
public class APIController {

    @Autowired
    private IProjectService projectService;
    @Autowired
    private VersionRepository versionRepository;
    @Autowired
    private ProjectManager projectManager;

    /**
     * 获取推荐项目
     * @return
     */
    @RequestMapping(value = "/getRecommend",method = RequestMethod.GET)
    public List<ProjectDto> getRecommend(){
        return this.limitTitleLength(projectService.getCommentProjectList2());
    }

    private List<ProjectDto> limitTitleLength(List<ProjectDto> list) {
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.stream().map(dto -> {
            if(dto.getTitle() != null && dto.getTitle().length() > 19){
                dto.setTitle(dto.getTitle().substring(0,19));
            }
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 获取最新项目
     * @return
     */
    @RequestMapping(value = "/getNewsProject",method = RequestMethod.GET)
    public List<ProjectDto> getNewsProject(Integer page,Integer limit){
        List<ProjectDto> result = new ArrayList<>();
        if(page == 1){
            List<ProjectDto> commentList = projectService.getCommentProjectList2();
            for (ProjectDto project : commentList) {
                project.setTitle("【推荐】"+project.getTitle());
            }
            result.addAll(commentList);
        }
        List<ProjectDto> newList = projectService.getNewProjectListPage(page,limit);
        if(!CollectionUtils.isEmpty(newList)){
            result.addAll(newList);
        }

        return this.limitTitleLength(result);
    }


    /**
     * 获取最羊毛列表
     * @return
     */
    @RequestMapping(value = "/getYangmaoList",method = RequestMethod.GET)
    public List<ProjectDto> getYangmaoList(Integer page,Integer limit){
        return this.limitTitleLength(projectService.getYangmaoListPage(page,limit,1));
    }


    /**
     * 获取首码项目
     * @return
     */
    @RequestMapping(value = "/getShoumaList",method = RequestMethod.GET)
    public List<ProjectDto> getShoumaList(Integer page,Integer limit){
        return this.limitTitleLength(projectService.getYangmaoListPage(page,limit,2));
    }

    /**
     * 获取区块链项目
     * @return
     */
    @RequestMapping(value = "/getBlockChainList",method = RequestMethod.GET)
    public List<ProjectDto> getBlockChainList(Integer page,Integer limit){
        return this.limitTitleLength(projectService.getYangmaoListPage(page,limit,12));
    }


    /**
     * 获取闲谈
     * @return
     */
    @RequestMapping(value = "/getNewsList",method = RequestMethod.GET)
    public List<ProjectDto> getNewsList(Integer page,Integer limit){
        return this.limitTitleLength(projectService.getYangmaoListPage(page,limit,10));
    }

    /**
     * 获取项目详细
     * @return
     */
    @RequestMapping(value = "/getProjectDesc",method = RequestMethod.GET)
    public ProjectDto getNewsProject(Integer projectId, HttpServletRequest request){
        projectManager.updateReadAmount(request,projectId, "APP");
        ProjectDto dto = projectService.getProjectById_forAndroid(projectId);
        return projectService.handleApp(dto);
    }

    /**
     * 获取最新安卓版本号
     * @return
     */
    @RequestMapping(value = "/getVersion",method = RequestMethod.GET)
    public String getVersion(){
        Optional<VersionEntity> optionalVersionEntity = versionRepository.findById(1);
        if (optionalVersionEntity.isPresent()) {
            return optionalVersionEntity.get().getVersion();
        }
        return null;
    }


}
